package com.xnew.service.impl;

import com.xnewtech.xplatform.swan.libs.mp.page.PageFactory;
import com.xnewtech.xplatform.swan.model.page.PageResult;
import com.xnew.entity.SysRelation;
import com.xnew.mapper.SysRelationMapper;
import com.xnew.model.params.SysRelationParam;
import com.xnew.model.result.SysRelationResult;
import  com.xnew.service.SysRelationService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 角色和菜单关联表 服务实现类
 * </p>
 *
 * @author shj
 * @since 2021-08-09
 */
@Service
public class SysRelationServiceImpl extends ServiceImpl<SysRelationMapper, SysRelation> implements SysRelationService {

    @Override
    public void add(SysRelationParam param){
        SysRelation entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(SysRelationParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(SysRelationParam param){
        SysRelation oldEntity = getOldEntity(param);
        SysRelation newEntity = getEntity(param);
        BeanUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysRelationResult findBySpec(SysRelationParam param){
        return null;
    }

    @Override
    public List<SysRelationResult> findListBySpec(SysRelationParam param){
        return null;
    }

    @Override
    public PageResult<SysRelationResult> findPageBySpec(SysRelationParam param){
        Page pageContext = getPageContext();
        IPage<SysRelationResult> page = this.baseMapper.customPageList(pageContext, param);
        return new PageResult<>(page);
    }

    private Serializable getKey(SysRelationParam param){
        return param.getRelationId();
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

    private SysRelation getOldEntity(SysRelationParam param) {
        return this.getById(getKey(param));
    }

    private SysRelation getEntity(SysRelationParam param) {
        SysRelation entity = new SysRelation();
        BeanUtil.copyProperties(param, entity);
        return entity;
    }

}
